home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / optivc32 / cfmath.h < prev    next >
C/C++ Source or Header  |  1999-03-06  |  16KB  |  468 lines

  1. /*    CFMATH.H
  2.  
  3.       Complex library for the languages C and C++.
  4.  
  5.       This header file contains all definitions for
  6.       single-precision complex numbers (complex float).
  7.  
  8.       Copyright (C) 1996-1999 Martin Sander
  9.       Address of the author:
  10.            Dr. Martin Sander Software Dev.
  11.            Sertuernerstr. 11
  12.            D-37085 Goettingen
  13.            Germany
  14.            MartinSander@Bigfoot.com
  15.            http://www.optivec.com
  16. */
  17.  
  18.  
  19. #ifndef __CFMATH_H
  20. #define __CFMATH_H
  21.  
  22. #if !defined( _CMATH_DEFS )
  23.    #ifdef __BORLANDC__
  24.        #pragma option -a-
  25.    #else /* Visual C++ */
  26.        #pragma pack( push,1 )
  27.    #endif /* avoid insertion of dummy bytes  */
  28.    typedef struct {float    Re, Im;} fComplex;
  29.    typedef struct {double   Re, Im;} dComplex;
  30.    #ifdef __BORLANDC__
  31.        typedef long double    extended;
  32.        typedef struct {extended Re, Im;} eComplex;
  33.        #pragma option -a.
  34.    #else /* Visual C++ */
  35.        typedef  double extended; /* Visual C++ does not support
  36.                                  80-bit IEEE numbers. So make
  37.                                  extended equal to double    */
  38.        typedef dComplex  eComplex;
  39.        #pragma pack( pop )
  40.    #endif    /* restore default data packing  */
  41.    typedef fComplex fcomplex;
  42.    typedef dComplex dcomplex;
  43.    typedef eComplex ecomplex;  // tolerate all-lower case
  44.    #define _CMATH_DEFS
  45. #endif
  46. #ifdef __BORLANDC__
  47.     #include <_defs.h>
  48.     #if defined __TINY || defined __SMALL__ || defined __MEDIUM__
  49.         #if defined(_RTLDLL) || defined(_CLASSDLL)
  50.             #error Must use static BC Runtime Library with OptiVec and CMATH in models TINY, SMALL, MEDIUM
  51.         #endif
  52.         #define   _VFAR  near   /* even in case of DS!=SS  */
  53.     #elif defined __FLAT__
  54.         #define  _VFAR
  55.     #else
  56.         #define   _VFAR  far
  57.     #endif
  58.     #if (__BORLANDC__ >= 0x450)
  59.          #define __cmf _RTLENTRY _EXPFUNC
  60.     #else
  61.          #define __cmf  _Cdecl _FARFUNC
  62.     #endif
  63. #else  /* Visual C++, Optima++ */
  64.     #define _VFAR
  65.     #define __cmf __cdecl
  66. #endif
  67. #define _VFARC const _VFAR
  68.  
  69. /*  first the constructors:  */
  70. #ifdef __cplusplus
  71.    /* since fComplex is declared as a struct instead of a class,
  72.       the constructor cannot get the name "fComplex" here.     */
  73.   #ifndef _FCPLX_DEFINED
  74.   inline fComplex __cmf fcplx( float __ReVal )
  75.   {   fComplex Result;
  76.       Result.Re = __ReVal;
  77.       Result.Im = 0.0f;
  78.       return Result;
  79.   }
  80.  
  81.       // down-conversions from double and extended precision
  82.       // (with OVERFLOW error handling):
  83.   fComplex __cmf fcplx( dComplex __zd );
  84.   #if defined __COMPLEX_H
  85.       fComplex __cmf fcplx( complex __z );
  86.   #endif
  87.   #ifdef __BORLANDC__
  88.       fComplex __cmf fcplx( eComplex __ze );
  89.   #endif
  90.   #define _FCPLX_DEFINED
  91.   #endif  // _FCPLX_DEFINED
  92. #endif  
  93.       /* basic form of constructor for C and C++ : */
  94.    fComplex __cmf fcplx( float __ReVal, float __ImVal);
  95.          /* down-conversion with OVERFLOW-handling:  */
  96. #if !defined __NEWCPLX_H
  97.     #if defined __cplusplus
  98.         extern "C" {
  99.     #endif
  100.     fComplex __cmf cdtocf( dComplex __zd );
  101.     fComplex __cmf cetocf( eComplex __ze );
  102.     #if defined __cplusplus
  103.         }
  104.     #endif
  105. #endif
  106.  
  107.    /* Basic complex operations. They are defined both
  108.    for C and C++. However, for C++ you may as well use the
  109.    overloaded operators and functions defined further below. */
  110. #define         cf_real( z )  (z).Re
  111. #define         cf_imag( z )  (z).Im
  112. #if defined __cplusplus && !defined _CMATH_CLASSDEFS 
  113. extern "C" {  // the following functions cannot be "extern C",
  114. #endif       // if fComplex identical with complex<float>
  115.  
  116. fComplex __cmf  cf_neg(  fComplex __z );
  117. fComplex __cmf  cf_conj( fComplex __z );
  118.  
  119. #ifdef __cplusplus  // even if _CMATH_CLASSDEFS
  120.     extern "C" float __cmf  cf_norm( fComplex __z );
  121.     extern "C" float __cmf  cf_arg(  fComplex __z );
  122. #else
  123.     float __cmf  cf_norm( fComplex __z );
  124.     float __cmf  cf_arg(  fComplex __z );
  125. #endif
  126. fComplex __cmf  cf_polar( float __mag, float __angle );
  127.  
  128. fComplex __cmf  cf_add(   fComplex __x, fComplex __y );
  129. fComplex __cmf  cf_addRe( fComplex __x, float __yRe );
  130. fComplex __cmf  cf_sub(   fComplex __x, fComplex __y );
  131. fComplex __cmf  cf_subRe( fComplex __x, float __yRe );  /* x - yRe */
  132. fComplex __cmf  cf_subrRe( fComplex __x, float __yRe ); /* yRe - x */
  133. fComplex __cmf  cf_mul(   fComplex __x, fComplex __y );
  134. fComplex __cmf  cf_mulRe( fComplex __x, float __yRe );
  135. fComplex __cmf  cf_div(   fComplex __x, fComplex __y );
  136. fComplex __cmf  cf_divRe( fComplex __x, float __yRe );   /*  x / yRe  */
  137. fComplex __cmf  cf_divrRe( fComplex __x, float __yRe );  /*  yRe / x  */
  138.  
  139. /*  mathematical functions with error handling through _matherr: */
  140. #ifdef __cplusplus  // even if _CMATH_CLASSDEFS
  141.     extern "C" float  __cmf  cf_abs(  fComplex __z );
  142. #else
  143.     float  __cmf  cf_abs(  fComplex __z );
  144. #endif
  145. fComplex __cmf  cf_acos( fComplex __z );
  146. fComplex __cmf  cf_asin( fComplex __z );
  147. fComplex __cmf  cf_atan( fComplex __z );
  148. fComplex __cmf  cf_cos(  fComplex __z );
  149. fComplex __cmf  cf_cosh( fComplex __z );
  150. fComplex __cmf  cf_cubic( fComplex __z );  /* raise to the third power */
  151. fComplex __cmf  cf_exp(  fComplex __z );
  152. fComplex __cmf  cf_inv(  fComplex __z );    /*   1.0 / z   */
  153. fComplex __cmf  cf_ipow( fComplex __z, int __exponent );
  154.                                              /* raise z to integer power */
  155. fComplex __cmf  cf_ln(    fComplex __z );
  156. fComplex __cmf  cf_log(   fComplex __z ); /* same as cf_ln */
  157. fComplex __cmf  cf_log2(  fComplex __z );
  158. fComplex __cmf  cf_log10( fComplex __z );
  159. fComplex __cmf  cf_pow( fComplex __base, fComplex __exponent );
  160. fComplex __cmf  cf_powReBase( float __base, fComplex __exponent ); /* power of real base */
  161. fComplex __cmf  cf_powReExpo( fComplex __base, float __exponent ); /* raise z to real power */
  162.                          /* for integer exponents, use cf_ipow ! */
  163. fComplex __cmf  cf_quartic( fComplex __z );  /* raise to the fourth power */
  164. fComplex __cmf  cf_sin(  fComplex __z );
  165. fComplex __cmf  cf_sinh( fComplex __z );
  166. fComplex __cmf  cf_square( fComplex __z );
  167. fComplex __cmf  cf_sqrt( fComplex __z );
  168. fComplex __cmf  cf_tan(  fComplex __z );
  169. fComplex __cmf  cf_tanh( fComplex __z );
  170.  
  171.  
  172. #if defined __cplusplus && !defined _CMATH_CLASSDEFS
  173. }    //  end of the extern "C" statement
  174. #endif
  175.  
  176. #if defined __cplusplus && !defined __STD_COMPLEX && !defined __NEWCPLX_H
  177.    /* in addition to the basic operations defined above for C,
  178.       here is the same complete set of overloaded operators and
  179.       functions as offered by <newcplx.h> for the complex classes.  */
  180.  
  181.     inline float real( fComplex _VFARC & __z )
  182.     {
  183.         return __z.Re;
  184.     }
  185.  
  186.     inline float imag( fComplex _VFARC & __z )
  187.     {
  188.         return __z.Im;
  189.     }
  190.  
  191.     inline fComplex neg( fComplex _VFARC & __z1 )
  192.     {   fComplex Result;
  193.         Result.Re = -__z1.Re;
  194.         Result.Im = -__z1.Im;
  195.         return Result;
  196.     }
  197.  
  198.     inline fComplex conj( fComplex _VFARC & __z)
  199.     {   fComplex Result;
  200.         Result.Re =  __z.Re;
  201.         Result.Im = -__z.Im;
  202.         return Result;
  203.     }
  204.  
  205.     float    __cmf norm( fComplex __z );
  206.     float    __cmf arg(  fComplex __z );
  207.     fComplex __cmf polar( float Mag, float Angle );
  208.  
  209.   //  unary operators:
  210.  
  211.   inline fComplex _VFAR & operator +( fComplex _VFAR & __z1 )
  212.   {
  213.       return __z1;
  214.   }
  215.  
  216.   inline fComplex operator -( fComplex _VFARC & __z1 )
  217.   {   fComplex Result;
  218.       Result.Re = -__z1.Re;
  219.       Result.Im = -__z1.Im;
  220.       return Result;
  221.   }
  222.  
  223.   //  binary operators:
  224.  
  225.   inline fComplex operator +( fComplex _VFARC & __z1, fComplex _VFARC & __z2 )
  226.   {   fComplex Result;
  227.       Result.Re = __z1.Re + __z2.Re;
  228.       Result.Im = __z1.Im + __z2.Im;
  229.       return Result;
  230.   }
  231.  
  232.   inline fComplex operator +( fComplex _VFARC & __z1, float __z2Re )
  233.   {   fComplex Result;
  234.       Result.Re = __z1.Re + __z2Re;
  235.       Result.Im = __z1.Im;
  236.       return Result;
  237.   }
  238.  
  239.   inline fComplex operator +( float __z1Re, fComplex _VFARC & __z2 )
  240.   {   fCompl